// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-

/// @file	AP_MotorsMatrix_dc.h
/// @brief	Motor control class for Matrixcopters

#ifndef AP_MOTORSMATRIX_DC
#define AP_MOTORSMATRIX_DC

#include <FastSerial.h>
#include <AP_Common.h>
#include <AP_Math.h>        // ArduPilot Mega Vector/Matrix math Library
#include <RC_Channel.h>     // RC Channel Library
#include <APM_RC.h>         // ArduPilot Mega RC Library
#include <AP_Motors_dc.h>

#define AP_MOTORS_MATRIX_MOTOR_UNDEFINED -1
#define AP_MOTORS_MATRIX_ORDER_UNDEFINED -1

#define AP_MOTORS_MATRIX_MOTOR_CW -1
#define AP_MOTORS_MATRIX_MOTOR_CCW 1

#define AP_MOTORS_MATRIX_YAW_LOWER_LIMIT_PWM    100

/// @class      AP_MotorsMatrix_dc
class AP_MotorsMatrix_dc : public AP_Motors_dc {
public:

    /// Constructor
    AP_MotorsMatrix_dc( uint8_t APM_version, APM_RC_Class* rc_out, RC_Channel* rc_roll, RC_Channel* rc_pitch, RC_Channel* rc_throttle, RC_Channel* rc_yaw, RC_Channel* rc_roll_gyro, RC_Channel* rc_pitch_gyro, uint16_t speed_hz = AP_MOTORS_SPEED_DEFAULT) :
        AP_Motors_dc(APM_version, rc_out, rc_roll, rc_pitch, rc_throttle, rc_yaw, rc_roll_gyro, rc_pitch_gyro, speed_hz),
        _num_motors(0) {
    };
	enum Type {besc, lesc, servo_roll, servo_pitch,servo_yaw, undefined};
    // init
    virtual void        Init();

    // set update rate to motors - a value in hertz
    // you must have setup_motors before calling this
    virtual void            set_update_rate( uint16_t speed_hz );

    // set frame orientation (normally + or X)
    virtual void            set_frame_orientation( uint8_t new_orientation );

    // enable - starts allowing signals to be sent to motors
    virtual void            enable();

    // get basic information about the platform
    virtual uint8_t         get_num_motors() {
        return _num_motors;
    };

    // motor test
    virtual void        output_test();

    // output_min - sends minimum values out to the motors
    virtual void        output_min();

    // add_motor using just position and prop direction
    virtual void        add_motor(int8_t motor_num, float angle_degrees, int8_t direction, int8_t testing_order = AP_MOTORS_MATRIX_ORDER_UNDEFINED, Type _type = undefined);

    // remove_motor
    virtual void        remove_motor(int8_t motor_num);

    // remove_all_motors - removes all motor definitions
    virtual void        remove_all_motors();

	virtual void		set_servo_angle_roll(int _angle);
	virtual void		set_servo_angle_pitch(int _angle);

    // setup_motors - configures the motors for a given frame type - should be overwritten by child classes
    virtual void        setup_motors() {
        remove_all_motors();
    };

    // matrix
    AP_Int8         test_order[AP_MOTORS_MAX_NUM_MOTORS];               // order of the motors in the test sequence

	 
	  RC_Channel      *_servo_1;
	int16_t _roll_out;
	int16_t _pitch_out;
    RC_Channel      *_servo_2;

	void Force_Out0_Out1(void);
	void Force_Out2_Out3(void);
	void Force_Out6_Out7(void);

	char servo_angle;
	float angle_factor_roll;
	float difference_roll;
	float angle_factor_pitch;
	float difference_pitch;
	float angle_factor_yaw;
	float difference_yaw;

	int16_t pwm_roll;
	int16_t pwm_pitch;
	int16_t pwm_yaw;

	int16_t pwm_roll_old;
	int16_t pwm;
	int16_t _pwm;
	int16_t pwm_old;
	int16_t _pwm_old;

	int speed;

	virtual void set_speed(int _speed);
	void set_factor_compensation(int _factor_compensation);
protected:
    // output - sends commands to the motors
    virtual void        output_armed();
    virtual void        output_disarmed();

    // add_motor using raw roll, pitch, throttle and yaw factors
    virtual void        add_motor_raw(int8_t motor_num, float roll_fac, float pitch_fac, float yaw_fac, int8_t testing_order = AP_MOTORS_MATRIX_ORDER_UNDEFINED, Type _type = undefined);

    int8_t              _num_motors; // not a very useful variable as you really need to check the motor_enabled array to see which motors are enabled
    float               _roll_factor[AP_MOTORS_MAX_NUM_MOTORS]; // each motors contribution to roll
    float               _pitch_factor[AP_MOTORS_MAX_NUM_MOTORS]; // each motors contribution to pitch
    float               _yaw_factor[AP_MOTORS_MAX_NUM_MOTORS];  // each motors contribution to yaw (normally 1 or -1)
	Type				_type[AP_MOTORS_MAX_NUM_MOTORS];
	float _roll_scaler;                         // scaler to convert roll input from radio (i.e. -4500 ~ 4500) to max roll range
    float _pitch_scaler;                        // scaler to convert pitch input from radio (i.e. -4500 ~ 4500) to max pitch range
    int16_t factor_compensation;
	int16_t roll_calc;
	int16_t pitch_yaw_calc;
	int16_t compensation;
	float roll_comp;
	float _roll_comp; 
	float pitch_yaw_comp;
	float pitch_comp;
	float yaw_comp;
	int16_t pwm_min_roll;
	int16_t pwm_max_roll;
	int16_t pwm_min_pitch;
	int16_t pwm_max_pitch;
};

#endif  // AP_MOTORSMATRIX_DC